package de.rj.urwalking.data.survey;

import java.io.File;
import java.util.ArrayList;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Environment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import de.rj.urwalking.R;
import de.rj.urwalking.config.Config;
import de.rj.urwalking.config.Constants;

/**
 * 
 * A Question that is answered by choosing one out of several images.
 * 
 * @author rjj
 * 
 */

public class ImageQuestion extends Question {

	private static final long serialVersionUID = 1L;
	private boolean isAnswered = false;

	public ImageQuestion(int id, int itemNumber, String text, int scaleId, int screen) {
		super(id, itemNumber, text, scaleId, screen);

	}

	@Override
	public View buildView(final Context context, ViewGroup parent) {

		LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		final RelativeLayout questionView = (RelativeLayout) inflater.inflate(R.layout.question_images, parent, false);

		TextView questionText = (TextView) questionView.findViewById(R.id.questionText);
		questionText.setText(getText());

		for (ScalePoint scalePoint : scalePoints) {
			new LoadImageTask(questionView).execute(scalePoint.getPictureId());
		}

		// register click listener for all ImageButtons
		for (ImageButton imageButton : findAllImageButtons(questionView)) {
			imageButton.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {

					deselectAllImageButtons(questionView);
					v.setSelected(true);

					isAnswered = true;

					int pictureId = Integer.valueOf(v.getTag().toString());
					updateAnswer(scale.getValueForPictureId(pictureId));
				}
			});
		}

		restoreState(questionView);

		return questionView;

	}

	protected ArrayList<ImageButton> findAllImageButtons(ViewGroup questionView) {

		ArrayList<ImageButton> imageButtons = new ArrayList<ImageButton>();
		for (int i = 0; i < questionView.getChildCount(); i++) {
			if (questionView.getChildAt(i) instanceof LinearLayout) {
				LinearLayout ll = (LinearLayout) questionView.getChildAt(i);
				for (int j = 0; j < ll.getChildCount(); j++) {
					if (ll.getChildAt(j) instanceof ImageButton) {
						imageButtons.add((ImageButton) ll.getChildAt(j));
					}
				}
			}
		}
		return imageButtons;

	}

	protected void deselectAllImageButtons(ViewGroup questionView) {

		for (ImageButton imageButton : findAllImageButtons(questionView)) {
			imageButton.setSelected(false);
		}
	}

	private void restoreState(View questionView) {

		int answerId = getAnswer().getIntAnswer();
		int pictureId = scale.getPictureIdForValue(answerId);
		if (pictureId != Constants.UNDEFINED) {
			ImageButton imageButton = (ImageButton) questionView.findViewWithTag(String.valueOf(pictureId));
			imageButton.setSelected(true);
			isAnswered = true;
			// force the activity to enable the "next" button
			mCallback.onAnswerChanged();			
		}

	}

	@Override
	public boolean hasBeenAnswered() {

		return isAnswered;
	}

	private class LoadImageTask extends AsyncTask<Integer, Void, Bitmap> {

		private int pictureId;
		private View questionView;

		public LoadImageTask(View questionView) {
			this.questionView = questionView;
		}

		@Override
		protected Bitmap doInBackground(Integer... params) {

			pictureId = params[0];

			String path = Environment.getExternalStorageDirectory() + File.separator + Config.FOLDER_NAME
					+ File.separator + pictureId + ".png";
			return BitmapFactory.decodeFile(path);
		}

		@Override
		protected void onPostExecute(Bitmap result) {

			ImageButton imageButton = (ImageButton) questionView.findViewWithTag(String.valueOf(pictureId));
			imageButton.setImageBitmap(result);

		}

	}

}
